package juc.aqs;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/*
* 
* CountDownLatch实现模拟游戏加载
* */
class Test7{
    public static void main(String[] args) throws InterruptedException {
        String[]speed = new String[10];
        Random random = new Random();
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int j = 0; j <10 ; j++) {
           final int s=j;
            new Thread(()->{
                for (int i = 0; i <=100 ; i++) {
                    speed[s]=i+"%";
                    try {
                        Thread.sleep(random.nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print("\r"+Arrays.toString(speed));
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.print("\r"+"欢迎来到王者荣耀");
    }
}
/*
*
* CountDownLatch 的使用
* */
public class Test5 {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread(()->{
               System.out.println(Thread.currentThread().getName()+"running...");
               try {
                   Thread.sleep(500);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println(Thread.currentThread().getName()+"ending...");
               countDownLatch.countDown();
           }).start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"running...");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"ending...");
            countDownLatch.countDown();
        }).start();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"running...");
            try {
                Thread.sleep(3500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"ending...");
            countDownLatch.countDown();
        }).start();
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("main ending...");
    }
}
/*
* countDownLatch 线程池使用
* */
class Test6{
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        ExecutorService threadPool = Executors.newFixedThreadPool(4);
        threadPool.submit(()->{
            System.out.println(Thread.currentThread().getName()+"running...");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"ending...");
            countDownLatch.countDown();
        });
        threadPool.submit(()->{
            System.out.println(Thread.currentThread().getName()+"running...");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"ending...");
            countDownLatch.countDown();
        });
        threadPool.submit(()->{
            System.out.println(Thread.currentThread().getName()+"running...");
            try {
                Thread.sleep(3500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"ending...");
            countDownLatch.countDown();
        });
        threadPool.submit(()->{
            System.out.println(Thread.currentThread().getName()+"等待汇总...");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"汇总完毕...");
        });
    }
}
